/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.chl.niclun.projectwebshop.nbp;


import edu.chl.niclun.projectwebshop.nbp.DataBase.Database;
import edu.chl.niclun.projectwebshop.nbp.Entities.Movie;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.event.ActionEvent;
import org.primefaces.event.RateEvent;

/**
 *backingbean for the movepopup.xhtml
 * @author Nume
 */
@ManagedBean(name ="MoviePopUpBean")
@ViewScoped
public class MoviePopUpBackingBean {
    private Movie selectedMovie;
    
    //instance of the cartbean
    @ManagedProperty(value="#{CartBean}")
    CartModelBean cartBean;
    
    
    public MoviePopUpBackingBean(){
        
    }

    public Movie getSelectedMovie() {
        return selectedMovie;
    }

    public void setSelectedMovie(Movie selectedMovie) {
        this.selectedMovie = selectedMovie;
    }

    public CartModelBean getCartBean() {
        return cartBean;
    }

    public void setCartBean(CartModelBean cartBean) {
        this.cartBean = cartBean;
    }
    
    
    //add a product to the cart
    public void addProduct(ActionEvent evt){
       
        cartBean.addToCart(selectedMovie);
        
        
    }
    //rate the movie
    public void handleRate(RateEvent rateEvent){
        
        double rating = selectedMovie.getRating()+ (rateEvent.getRating()/50);
        rating = ((int)(rating*10))/10.0;
        if(rating >= 10.0 )
            selectedMovie.setRating(10.0);
        else
            selectedMovie.setRating(rating);
        
        try{
            Database.getWebshopController().edit(selectedMovie);
        }
        catch(Exception e){
            
        }
    }
    
    
    
    
}
